import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '@/views/login'
// import Layout from '@/views/layout/index.vue'
// import Home from '@/views/home/index.vue'
// import User from '@/views/user/index.vue'
// import Search from '@/views/search/index.vue'
// import SearchResult from '@/views/search/searchResult.vue'
// import ArticleDetail from '@/views/articleDetail/index.vue'
// import UserEdit from '@/views/user/userEdit.vue'
// import Chat from '@/views/chat/index.vue'

import {getToken} from '../utils/token.js'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: '/layout/home'
  },
  {
    path: '/followe_up',
    component:() => import(/* webpackChunkName: "login" */ '@/views/user/followeUp.vue')
  },
  {
    path: '/start_article',
    component:() => import(/* webpackChunkName: "login" */ '@/views/articleDetail/start.vue')
  },
  {
    path: '/login',
    component:() => import(/* webpackChunkName: "login" */ '@/views/login'),
    // 独享守卫
    beforeEnter(to,from,next){
        if(getToken()?.length > 0){
          console.log('已经登录了')
          next('/')
        }else{
          next()
        }
    }
  },
  {
    path: '/layout',
    component:() => import(/* webpackChunkName: "layout" */ '@/views/layout/index.vue'),
    children: [{
        path: 'home',
        component:() => import(/* webpackChunkName: "home" */ '@/views/home/index.vue'),
        meta:{
          'scrollT':0
        },
      },
      {
        path: 'user',
        component:() => import(/* webpackChunkName: "user" */ '@/views/user/index.vue'),
      },
    ]
  },
  {
    path: '/search',
    component:() => import(/* webpackChunkName: "search" */ '@/views/search/index.vue')
  },
  {
    name:'search_result',
    path: '/search_result/:kw',
    component:() => import(/* webpackChunkName: "search_result" */ '@/views/search/searchResult.vue')
  },
  {
    path: '/article_detail',
    component:() => import(/* webpackChunkName: "article_detail" */ '@/views/articleDetail/index.vue')
  },
  {
    path: '/user_edit',
    component:() => import(/* webpackChunkName: "user_edit" */ '@/views/user/userEdit.vue')
  },
  {
    path: '/chat',
    component:() => import(/* webpackChunkName: "chat" */ '@/views/chat/index.vue')
  }
]
const router = new VueRouter({
  routes
})

router.beforeEach((to,form,next)=>{
  if(getToken()?.length > 0 && to.path == '/login'){
    console.log('已经登录了')
    next('/')
  }else{
    next()
  }
  // 存储当前地址的url
  // localStorage.setItem('url',form.fullPath)
})

export default router
